package com.renchunlin.taobaounion.presenter.impl;

import android.util.Log;

import com.renchunlin.taobaounion.model.Api;
import com.renchunlin.taobaounion.model.domain.TicketParams;
import com.renchunlin.taobaounion.model.domain.TicketResult;
import com.renchunlin.taobaounion.presenter.ITicketPresenter;
import com.renchunlin.taobaounion.utils.LogUtils;
import com.renchunlin.taobaounion.utils.RetrofitManage;
import com.renchunlin.taobaounion.utils.UrlUtils;
import com.renchunlin.taobaounion.view.ITicketPagerCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/1/19.
 * PS: Not easy to write code, please indicate.
 */
public class TicketPresenterImpl implements ITicketPresenter {

    private ITicketPagerCallback mViewCallback = null;
    private String mCover = null;
    private TicketResult mTicketResult;

    private enum LoadState {
        LOADING, SUCCESS, ERROR, NONE
    }

    private LoadState mCurrentState = LoadState.NONE;

    @Override
    public void getTicket(String title, String url, String cover) {
        this.onTicketLoading();
        this.mCover = UrlUtils.getCoverPath(cover);

        LogUtils.d(this, "title-->" + title);
        LogUtils.d(this, "url-->" + url);
        LogUtils.d(this, "cover-->" + cover);
        String ticketUrl = UrlUtils.getTicket(url);

        //去获取淘口令
        Retrofit retrofit = RetrofitManage.getInstance().getRetrofit();
        Api api = retrofit.create(Api.class);
        TicketParams ticketParams = new TicketParams(ticketUrl, title);
        Call<TicketResult> task = api.getTicket(ticketParams);
        task.enqueue(new Callback<TicketResult>() {
            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                int code = response.code();
                Log.i("chen", "onResponse: " + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    //请求成功
                    mTicketResult = response.body();
                    Log.i("chen", "onResponse: " + mTicketResult);
                    //通知ui更新
                    onTicketLoadedSuccess();
                } else {
                    //请求失败
                    onLoadedTicketError();
                }
            }

            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                onLoadedTicketError();
            }
        });
    }

    private void onTicketLoadedSuccess() {
        if (mViewCallback != null) {
            mViewCallback.onTicketLoaded(mCover, mTicketResult);
        } else {
            mCurrentState = LoadState.SUCCESS;
        }
    }

    private void onLoadedTicketError() {
        if (mViewCallback != null) {
            mViewCallback.onError();
        } else {
            mCurrentState = LoadState.ERROR;
        }
    }

    @Override
    public void registerViewCallback(ITicketPagerCallback callback) {
        this.mViewCallback = callback;
        if (mCurrentState != LoadState.NONE) {
            //说明状态已经改变了
            //更新ui
            if (mCurrentState == LoadState.SUCCESS) {
                //通知成功
                onTicketLoadedSuccess();
            } else if (mCurrentState == LoadState.ERROR) {
                onLoadedTicketError();
            } else if (mCurrentState == LoadState.LOADING) {
                onTicketLoading();
            }
        }
    }

    private void onTicketLoading() {
        if (mViewCallback != null) {
            mViewCallback.onLoading();
        } else {
            mCurrentState = LoadState.LOADING;
        }
    }

    @Override
    public void unregisterViewCallback(ITicketPagerCallback callback) {
        this.mViewCallback = null;
    }
}
